package org.example.Java基础.多线程与锁;


import org.example.handwriting.spring.ApplicationContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class 创建线程的方式 {
    private static final Logger logger = LoggerFactory.getLogger(创建线程的方式.class);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        // 使用Thread类 创建线程
        Thread thread1 = new Thread("t1") {
            int max = 10;
            int idx = 1;

            @Override
            public void run() {
                while (idx <= max) {
                    idx++;
                    logger.debug("t1");
//                    System.out.println(Thread.currentThread().getName() + "打印了:666");
                }
            }
        };

        // 使用Runnable配合Thread类 创建线程
        Runnable runnable = new Runnable() {
            int max = 10;
            int idx = 1;
            @Override
            public void run() {
                while (idx <= max) {
                    idx++;
                    logger.debug("t2");
//                    System.out.println(Thread.currentThread().getName() + "打印了:666");
                }
            }
        };
        Thread thread2 = new Thread(runnable,"t2");


        // 使用FutureTask类 实现Callable接口 配合Thread类 创建线程 可以获取返回值
        FutureTask<String> futureTask = new FutureTask<>(new Callable<String>() {
            @Override
            public String call() throws Exception {
                logger.debug("t3");
                return "你好";
            }
        });
        Thread thread3 = new Thread(futureTask, "t3");
        
        thread1.start();
        thread2.start();
        thread3.start();

        String res = futureTask.get();
        System.out.println(res);
    }
}
